<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Camp;
use App\Models\Label;
use App\Models\PreTime;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\Storage;

class CampController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Camp(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('camp_name');
            $grid->column('camp_img')->display(function ($img) {
                $i = Storage::url(explode(',', $img)[0] ?? '');
                return "<img src={$i} width='50'>";
            });
            $grid->column('camp_price');
            $grid->column('camp_tel');
            $grid->camp_qrcode->image('', 50);
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('stock');
            $grid->column('sales');
//            $grid->column('日期列表')->display('View')->modal('日期列表', \App\Admin\Support\PreTime::make(['type' => PreTime::TYPE_DATE, 'rel_type' => PreTime::TYPE_DATE]));
            $grid->column('日期列表')->display('View')->modal('日期列表', function () {
//                return '<a href="pre_time?type=' . PreTime::TYPE_DATE . '&rel_type=' . PreTime::TYPE_DATE . '&rel_id=1">日期列表</a>';
                return '<iframe data-src="pre_time?type=' . PreTime::TYPE_DATE . '&rel_type=' . PreTime::TYPE_DATE . '&rel_id=1&iframe=1' . '" width="100%" height="600" style="height:80vh" frameborder="0" id="scan-iframe-' . $this->id . '"></iframe>';
            });
//            $grid->column('日期列表')->display(function () {
//                return '<a href="pre_time?type=' . PreTime::TYPE_DATE . '&rel_type=' . PreTime::TYPE_DATE . '&rel_id=1">日期列表</a>';
//            });
            $grid->column('sort')->sortable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('camp_name');
                $filter->like('camp_tel');
                $filter->between('start_time')->datetime();
                $filter->between('end_time')->datetime();
                $filter->between('created_at')->datetime();
                $filter->between('updated_at')->datetime();
            });
            $grid->quickSearch(['id', 'camp_name']);
            // 禁用
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Camp(), function (Show $show) {
            $show->field('id');
            $show->field('camp_name');
            $show->field('camp_desc');
            $show->field('camp_img');
            $show->field('camp_price');
            $show->field('camp_tel');
            $show->field('camp_qrcode');
            $show->field('camp_details');
            $show->field('label_ids');
            $show->field('buy_notice');
            $show->field('lat');
            $show->field('lng');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('stock');
            $show->field('sales');
            $show->field('eat_name');
            $show->field('eat_lat');
            $show->field('eat_lng');
            $show->field('eat_time');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Camp(), function (Form $form) {
            $form->display('id');
            $form->text('camp_name')->rules('required|max:100');
            $form->text('camp_desc')->rules('required');
            $form->multipleImage('camp_img')->autoUpload()->removable(false)->retainable()->saving(function ($value) {
                return implode(',', $value);
            })->rules('required');
            $form->decimal('camp_price')->type('float')->rules('required');
            $form->tel('camp_tel')->rules('required');
            $form->image('camp_qrcode')->autoUpload()->removable(false)->retainable();
            $form->editor('camp_details');
            $form->keyValue('buy_notice')->setKeyLabel('标题')->setValueLabel('内容')->saveAsJson();
            $form->multipleSelect('label_ids')->options(Label::all()->pluck('label_name', 'id'))->saving(function ($value) {
                return implode(',', $value);
            });
            $form->text('lat');
            $form->text('lng')->help('<a href="http://guihuayun.com/maps/getxy.php" target="_blank">获取“高德坐标”</a>');
            $form->datetimeRange('start_time', 'end_time', '活动时间');
            $form->number('stock')->type('number');
            $form->text('eat_name');
            $form->text('eat_lat');
            $form->text('eat_lng')->help('<a href="http://guihuayun.com/maps/getxy.php" target="_blank">获取“高德坐标”</a>');
            $form->text('eat_time');
            $form->number('sort')->type('number');
        });
    }
}
